package com.fit.dao.vehicle;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.entities.LicencePlate;
import com.fit.entities.User;
import com.fit.entities.Vehicle;

public class VehicleDAOImpl implements VehicleDAO {

	@Inject
	private Session session;
	
	public boolean save(Vehicle vehicle) {
		if (vehicle != null) {
			session.save(vehicle);
			return true;
		}
		return false;
	}

	public Vehicle findByChassisNumber(String chassisNumber) {
		ArrayList<Vehicle> list = (ArrayList<Vehicle>) session.createCriteria(Vehicle.class).add(Restrictions.eq("chassisNumber", chassisNumber)).list();
		if(list.size()==0){
			return null;
		} 
		return list.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List<Vehicle> findAll() {
		return session.createCriteria(Vehicle.class).list();
	}
	
	
	public boolean update(Vehicle vehicle) {
		if (vehicle != null) {
			session.update(vehicle);
			return true;
		}
		return false;
	}

	public boolean deleteByChassisNumber(String chassisNumber) {
		Vehicle v = findByChassisNumber(chassisNumber);
		if (v != null) {
			session.delete(v);
			return true;
		}
		return false;
	}
	
	public boolean checkRegistrations(Vehicle vehicle) {
		ArrayList<LicencePlate> list = (ArrayList<LicencePlate>) session.createCriteria(LicencePlate.class).add(Restrictions.eq("vehicle", vehicle)).list();
		if(list.size()!=0){
			return true;
		}
		return false;
	}

}
